<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/20
 * Time: 11:28
 * BannerRepository.php
 */


namespace App\Repositories;


use App\Models\Supplier;
use Carbon\Carbon;

class SupplierRepository extends BaseRepositories
{
    /**
     * @var Supplier 注入的 model
     */
    protected $supplier;

    /**
     * AuctionRuleRepository constructor.
     * @param Supplier $supplier
     */
    public function __construct(Supplier $supplier)
    {
        $this->supplier = $supplier;
    }

    /**
     * @param array $data
     * @param int $limit
     * @return mixed
     */
    public function getSupplierPageRepository(array $data, $limit = LIMIT_PAGE){
        return $this->supplier
            ->where($data)
            ->paginate($limit);
    }

    /**
     * 更新供应商
     * @param array $data
     * @param int $categoryId
     * @return mixed
     */
    public function updateSupplierRepository(array $data){
        return $this->supplier->where('id', $data['id'])->update($data);
    }

    /**
     * 删除供应商
     * @param int $id
     * @return mixed
     */
    public function deleteSupplierByIdRepository($id){
        return $this->supplier->where(['id'=>$id])->delete();
    }

    /**
     * 保存供应商
     * @param array $data
     * @return Supplier
     */
    public function addSupplierRepository(array $data){
        //保存信息
        $auctionInfo = [
            'supplier_name'=>data_get($data, 'supplier_name', ''),
            'name' =>data_get($data, 'name', ''),
            'phone' =>data_get($data, 'phone', ''),
            'merchants'=>data_get($data, 'merchants', ''),
            'address'=>data_get($data, 'address', 0),
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now()
        ];

        return $this->supplier->create($auctionInfo);

    }
    /**
     * 获取拍卖规则
     * @param $ruleKey
     * @return mixed
     */
    public function getSupplierByIdRepository($id){
        return $this->supplier
            ->where(['id'=>$id])
            ->orderByDesc('updated_at')
            ->orderByDesc('created_at')
            ->first();
    }

    /**
     * @return mixed
     */
    public function getSupplierListRepository(){
        return $this->supplier
            ->where()
            ->get(['id', 'supplier_name']);
    }
}
